﻿using System;
using System.Windows.Input;
using System.Windows;
using WickedFlame.Core.Shapes;

namespace WickedFlame.Core.Tools
{
	public class LineTool : FactoryTool
	{
        public LineTool()
            : base()
        {
        }

        protected override VectorShape CreateShape(Point mousePosition)
        {
            var shape = new LineShape(mousePosition.X, mousePosition.Y, mousePosition.X, mousePosition.Y);

            shape.StrokeThickness = ShapePropertySettings.Settings.StrokeThickness;
            shape.Stroke = ShapePropertySettings.Settings.Stroke;
            //shape.Opacity = 255;

            return shape;
        }

        public override void OnMouseLeftButtonUp(DrawingCanvas sender, MouseButtonEventArgs e)
        {
            //Add object to history
            var shape = sender.SelectedLayer.ShapeList.TopShape as LineShape;
            if (shape != null)
            {
                sender.PushHistoryCommand(new WickedFlame.Core.History.AddCommand(shape, sender.SelectedLayerIndex));
                shape.CallChangeEndHandler();
            }
		}

        public override void OnMouseMove(DrawingCanvas sender, MouseEventArgs e)
		{
            if (e.LeftButton == MouseButtonState.Pressed)
            {
                Point m = Mouse.GetPosition(sender);
                var shape = sender.SelectedLayer.ShapeList.TopShape as LineShape;
                if (shape != null)
                {
                    foreach (VectorShape s in sender.SelectedLayer.ShapeList)
                    {
                        int n = s.HitTestInt(m);
                        if (n>0)
                        {
                            //check if point is near
                            m = s.GetTracker(n);
                        }
                    }
                    shape.MoveTracker(m, 2);
                }
            }
		}
	}
}
